﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Pwned
{
    public static class Info
    {
        [DllImport("kernel32.dll")]
        private static extern long GetVolumeInformation(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize);

        private static String getSerialNumber(String drive)
        {
            String result = "";
            try
            {
                string fileName = Path.GetTempFileName() + ".vbs";
                StreamWriter writer = new StreamWriter(fileName);
                String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                            + "Set colDrives = objFSO.Drives\n"
                            + "Set objDrive = colDrives.item(\"" + drive + "\")\n"
                            + "Wscript.Echo objDrive.SerialNumber"; 
                writer.Write(vbs);
                writer.Close();

                ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\System32\cscript.exe", "//NoLogo " + fileName);
                info.RedirectStandardOutput = true;
                info.UseShellExecute = false;
                info.WindowStyle = ProcessWindowStyle.Normal;
                info.CreateNoWindow = true;
                Process p = Process.Start(info);
                p.Start();

                StreamReader reader = p.StandardOutput;

                result = reader.ReadToEnd();

                reader.Close();
                File.Delete(fileName);
            }
            catch
            {
                result = "";                
            }

            result = result.Trim();
            if (result.Length > 3)
                result = result.Substring(0, 3);
            return result;

        }

        private static string getMotherboardSN()
        {
            String result = "";
            try
            {
                string fileName = Path.GetTempFileName() + ".vbs";
                StreamWriter writer = new StreamWriter(fileName);
                String vbs =
                   "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                  + "Set colItems = objWMIService.ExecQuery _ \n"
                  + "   (\"Select * from Win32_BaseBoard\") \n"
                  + "For Each objItem in colItems \n"
                  + "    Wscript.Echo objItem.SerialNumber \n"
                  + "    exit for  ' do the first cpu only! \n"
                  + "Next \n";
                writer.Write(vbs);
                writer.Close();

                ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\System32\cscript.exe", "//NoLogo " + fileName);
                info.RedirectStandardOutput = true;
                info.UseShellExecute = false;
                info.WindowStyle = ProcessWindowStyle.Normal;
                info.CreateNoWindow = true;
                Process p = Process.Start(info);

                StreamReader reader = p.StandardOutput;
                result = reader.ReadToEnd();

                reader.Close();
                File.Delete(fileName);
            }
            catch
            {
                result = "";
            }
            result = result.Trim();
            if (result.Length > 3)
                result = result.Substring(0, 3);
            return result;
        }

        private static string GetVolumeSerial(string strDriveLetter)
        {
            
            uint serNum = 0;
            uint maxCompLen = 0;
            try
            {
                StringBuilder VolLabel = new StringBuilder(256); // Label
                UInt32 VolFlags = new UInt32();
                StringBuilder FSName = new StringBuilder(256); // File System Name
                strDriveLetter += ":\\"; // fix up the passed-in drive letter for the API call
                long Ret = GetVolumeInformation(strDriveLetter, VolLabel, (UInt32)VolLabel.Capacity, ref serNum, ref maxCompLen, ref VolFlags, FSName, (UInt32)FSName.Capacity);
            }
            catch
            {
            }

            string result = serNum.ToString().Trim();
            if (result.Length > 3)
                result = result.Substring(0, 3);
            return result;
        }

        public static string GetUID()
        {
            try
            {
                string mbSerial = getMotherboardSN().ToLower();
                string volumeSerial = GetVolumeSerial("C").ToLower();
                string hddSerial = getSerialNumber("C").ToLower();

                string uid = mbSerial + volumeSerial + hddSerial + (Environment.ProcessorCount + Environment.MachineName + Environment.UserName).Substring(0, 8);

                if (uid.Length > 8)
                    uid = uid.Substring(0, 8);
                while (uid.Length < 8)
                    uid += "0";

                return uid;
            }
            catch
            {
                return (Environment.ProcessorCount + Environment.MachineName + Environment.UserName + "1s4df6").Substring(0,8);
            }
            
        }
    }
}
